package com.mc.search;

/**
 * @Description: 二分查找
 * @Author: MoChen
 */
public class BinSearch {
    static int[] arrs = {10, 20, 30, 40, 50};

    public static void main(String[] args) {
        System.out.println(bin_search(arrs, 10));
    }

    /**
     * 此处思路与教材思路不一致，教材中arrs[0]用于存储哨兵，可避免for循环中的下标越界
     */
    static boolean bin_search(int[] arrs, int num){
        int low = 0, high = arrs.length - 1, mid = 0;
        while(low <= high){
            mid = (low + high) / 2;
            if(arrs[mid] == num){
                return true;
            }else if(arrs[mid] > num){
                high = mid - 1;
            }else{
                low = mid + 1;
            }
        }
        return false;
    }
}
